package com.itheima.myreggie2.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.myreggie2.dao.CategoryDao;
import com.itheima.myreggie2.entity.Category;
import com.itheima.myreggie2.entity.Dish;
import com.itheima.myreggie2.entity.Setmeal;
import com.itheima.myreggie2.service.ICategoryService;
import com.itheima.myreggie2.service.IDishService;
import com.itheima.myreggie2.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

// 删除菜品分类 套餐分类
@Service
public class CategoryServiceImpl extends
        ServiceImpl<CategoryDao, Category> implements ICategoryService {

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    IDishService dishService;

    @Autowired
    ISetmealService setmealService;

    @Override
    public boolean deleteById(Long id) {
        //首先判断下当前分类的类型是什么（操作分类表）
        Category category = this.getById(id);
        //如果是1，表示菜品分类，接下来就查询dish(菜品表)
        //1.1查询dish（菜品表）：select count(*) from dish where category_id =?(
        //1.2 查询的数量大于0，直接返回false

        if (category.getType() == 1) {  //如果是1，表示菜品分类
            //创建条件构造器
            LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
            dishWrapper.eq(Dish::getCategoryId, id);

            int count = dishService.count(dishWrapper);
            if (count > 0) {
                //1.2 查询的数量大于0，直接返回false
                return false;
            }
        }
            //如果是2，表示套餐分类 , 接下来查询setmeal(套餐表)
            //2.1查询setmeal(套餐表)：select count(*) from dish where category_id =?
            //2.2 查询的数量大于0，直接返回false (count>0，说明菜品里面有定西，不能删除)
            if (category.getType() == 2) { //如果是2，表示套餐分类

                LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
                setmealWrapper.eq(Setmeal::getCategoryId, id);

                int count1 = setmealService.count(setmealWrapper);
                //2.2 查询的数量大于0，直接返回false (count>0，说明菜品里面有定西，不能删除)
                if (count1 > 0) {
                    return false;
                }
            }
                //3.执行删除操作
                //若上面操作都没走，直接进行删除操作  delete from category where id=
                return true;
            }

        }